package uranium.gui;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import uranium.db.DBManager;
import uranium.db.GestionePassword;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.Dimension;
import javax.swing.WindowConstants;

/**
 * @author hunterbit
 *
 */
public class Login extends JDialog
{

	private JPanel	jContentPane	= null;
	private JButton btnLogin = null;
	private JLabel lblUser = null;
	private JTextField txtUser = null;
	private JLabel lblPass = null;
	private JPasswordField txtPsw = null;
	
	private DBManager dbm=null;
	private JButton jButton = null;
	private JFrame padre = null;

	/**
	 * This is the default constructor
	 */
	public Login(DBManager dbm,JFrame padre)
	{
		super(padre,true);
		this.dbm=dbm;
		this.padre=padre;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(284, 174);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);  // Generated
		this.setTitle("Log-in");  // Generated
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
	}
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}
	
	public void windowClosed(WindowEvent arg0){
		try{
			//this.cm.closeConnection();
			//this.dbm.closeDB();
			padre.dispose();
			//rimuoviLock();
		}
		catch(Exception ea){
			ea.printStackTrace();
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			lblPass = new JLabel();
			lblPass.setBounds(new java.awt.Rectangle(10,57,101,16));  // Generated
			lblPass.setText("Password");  // Generated
			lblUser = new JLabel();
			lblUser.setBounds(new java.awt.Rectangle(10,25,101,16));  // Generated
			lblUser.setText("UserID");  // Generated
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBtnLogin(), null);  // Generated
			jContentPane.add(lblUser, null);  // Generated
			jContentPane.add(getTxtUser(), null);  // Generated
			jContentPane.add(lblPass, null);  // Generated
			jContentPane.add(getTxtPsw(), null);  // Generated
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes btnLogin	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnLogin()
	{
		if (btnLogin == null)
		{
			try
			{
				btnLogin = new JButton();
				btnLogin.setBounds(new Rectangle(145, 95, 90, 30));  // Generated
				btnLogin.setText("Login");  // Generated
				btnLogin.addActionListener(new java.awt.event.ActionListener()
				{
					public void actionPerformed(java.awt.event.ActionEvent e)
					{
						login();
					}
				});
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return btnLogin;
	}

	/**
	 * 
	 */
	private void login()
	{
		boolean ok=false;
		
			String user=txtUser.getText();
			char[] psw=txtPsw.getPassword();
			String pass=new String(psw);
			ok=controlloPassword(user,pass);
			if(!ok)
			{
				JOptionPane.showMessageDialog(this, "Password o UserID errati", "ERRORE LOG-IN", JOptionPane.ERROR_MESSAGE);
			}
		
		if(ok)dispose();	
	}

	/**
	 * @param user
	 * @param pass
	 * @return
	 */
	private boolean controlloPassword(String user, String pass)
	{
		GestionePassword gp=new GestionePassword(dbm);
		return gp.loginUtente(user, pass);
	}

	/**
	 * This method initializes txtUser	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtUser(){
		if (txtUser == null){
		//	try{
				txtUser = new JTextField();
				txtUser.setBounds(new Rectangle(130, 22, 131, 22));  // Generated
			//} 
			//catch (java.lang.Throwable e){
				// TODO: Something
			//}
		}
		return txtUser;
	}

	/**
	 * This method initializes txtPsw	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getTxtPsw()
	{
		if (txtPsw == null)
		{
			try
			{
				txtPsw = new JPasswordField();
				txtPsw.setBounds(new java.awt.Rectangle(130,54,131,22));  // Generated
				txtPsw.addKeyListener(new java.awt.event.KeyAdapter() {
					public void keyPressed(java.awt.event.KeyEvent e) {
						if (e.getKeyCode() == KeyEvent.VK_ENTER){
							login();
						}
					}
				});
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return txtPsw;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(30, 95, 90, 30));
			jButton.setText("Annulla");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					padre.dispose();
				}
			});
		}
		return jButton;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
